package com.yuetong.business.service.impl;

import java.util.List;
import com.yuetong.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Map;

import com.yuetong.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yuetong.business.domain.YtAssignedTask;
import com.yuetong.business.mapper.YtMaintenanceRecordMapper;
import com.yuetong.business.domain.YtMaintenanceRecord;
import com.yuetong.business.service.IYtMaintenanceRecordService;

/**
 * 维修记录，用于存储电梯的维护记录Service业务层处理
 *
 * @author  卓晓君
 * @date 2025-02-04
 */
@Service
public class YtMaintenanceRecordServiceImpl implements IYtMaintenanceRecordService
{
    @Autowired
    private YtMaintenanceRecordMapper ytMaintenanceRecordMapper;

    /**
     * 查询维修记录，用于存储电梯的维护记录
     *
     * @param recordId 维修记录，用于存储电梯的维护记录主键
     * @return 维修记录，用于存储电梯的维护记录
     */
    @Override
    public YtMaintenanceRecord selectYtMaintenanceRecordByRecordId(Long recordId)
    {
        return ytMaintenanceRecordMapper.selectYtMaintenanceRecordByRecordId(recordId);
    }

    /**
     * 查询维修记录，用于存储电梯的维护记录列表
     *
     * @param ytMaintenanceRecord 维修记录，用于存储电梯的维护记录
     * @return 维修记录，用于存储电梯的维护记录
     */
    @Override
    public List<YtMaintenanceRecord> selectYtMaintenanceRecordList(YtMaintenanceRecord ytMaintenanceRecord)
    {
        return ytMaintenanceRecordMapper.selectYtMaintenanceRecordList(ytMaintenanceRecord);
    }

    /**
     * 新增维修记录，用于存储电梯的维护记录
     *
     * @param ytMaintenanceRecord 维修记录，用于存储电梯的维护记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertYtMaintenanceRecord(YtMaintenanceRecord ytMaintenanceRecord)
    {
        ytMaintenanceRecord.setCreateTime(DateUtils.getNowDate());
        int rows = ytMaintenanceRecordMapper.insertYtMaintenanceRecord(ytMaintenanceRecord);
        insertYtAssignedTask(ytMaintenanceRecord);
        return rows;
    }

    /**
     * 修改维修记录，用于存储电梯的维护记录
     *
     * @param ytMaintenanceRecord 维修记录，用于存储电梯的维护记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateYtMaintenanceRecord(YtMaintenanceRecord ytMaintenanceRecord)
    {
        ytMaintenanceRecord.setUpdateTime(DateUtils.getNowDate());
        ytMaintenanceRecordMapper.deleteYtAssignedTaskByRecordId(ytMaintenanceRecord.getRecordId());
        insertYtAssignedTask(ytMaintenanceRecord);
        return ytMaintenanceRecordMapper.updateYtMaintenanceRecord(ytMaintenanceRecord);
    }

    /**
     * 批量删除维修记录，用于存储电梯的维护记录
     *
     * @param recordIds 需要删除的维修记录，用于存储电梯的维护记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYtMaintenanceRecordByRecordIds(Long[] recordIds)
    {
        ytMaintenanceRecordMapper.deleteYtAssignedTaskByRecordIds(recordIds);
        return ytMaintenanceRecordMapper.deleteYtMaintenanceRecordByRecordIds(recordIds);
    }

    /**
     * 删除维修记录，用于存储电梯的维护记录信息
     *
     * @param recordId 维修记录，用于存储电梯的维护记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYtMaintenanceRecordByRecordId(Long recordId)
    {
        ytMaintenanceRecordMapper.deleteYtAssignedTaskByRecordId(recordId);
        return ytMaintenanceRecordMapper.deleteYtMaintenanceRecordByRecordId(recordId);
    }
//维修类型次数折线图
    @Override
    public List<Map> selectLineMaintenance() {
        return ytMaintenanceRecordMapper.selectLineMaintenance() ;
    }

    /**
     * 新增指派维修任务，用于记录维修任务的指派和完成情况信息
     *
     * @param ytMaintenanceRecord 维修记录，用于存储电梯的维护记录对象
     */
    public void insertYtAssignedTask(YtMaintenanceRecord ytMaintenanceRecord)
    {
        List<YtAssignedTask> ytAssignedTaskList = ytMaintenanceRecord.getYtAssignedTaskList();
        Long recordId = ytMaintenanceRecord.getRecordId();
        if (StringUtils.isNotNull(ytAssignedTaskList))
        {
            List<YtAssignedTask> list = new ArrayList<YtAssignedTask>();
            for (YtAssignedTask ytAssignedTask : ytAssignedTaskList)
            {
                ytAssignedTask.setRecordId(recordId);
                list.add(ytAssignedTask);
            }
            if (list.size() > 0)
            {
                ytMaintenanceRecordMapper.batchYtAssignedTask(list);
            }
        }
    }
}
